<?php

namespace backend\models;

use Yii;

/**
 * This is the model class for table "auth_item".
 *
 * @property string $name
 * @property integer $type
 * @property string $description
 * @property string $rule_name
 * @property string $data
 * @property integer $created_at
 * @property integer $updated_at
 *
 * @property AuthAssignment[] $authAssignments
 * @property AuthRule $ruleName
 * @property AuthItemChild[] $authItemChildren
 * @property AuthItemChild[] $authItemChildren0
 */
class AuthItem extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'auth_item';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name', 'type','description'], 'required'],
            [['name'], 'unique','message'=>'不行，已经有了这个管理员'],
            [['type', 'created_at', 'updated_at'], 'integer'],
            [['description', 'data'], 'string'],
            [['name', 'rule_name'], 'string', 'max' => 64]
        ];

    }


    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'name' => '名字',
            'type' => 'Type',
            'description' => '描述',
            'rule_name' => 'Rule Name',
            'data' => 'Data',
            'created_at' => '创建时间',
            'updated_at' => '最后编辑时间',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getAuthAssignments()
    {
        return $this->hasMany(AuthAssignment::className(), ['item_name' => 'name']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getRuleName()
    {
        return $this->hasOne(AuthRule::className(), ['name' => 'rule_name']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getAuthItemChildren()
    {
        return $this->hasMany(AuthItemChild::className(), ['parent' => 'name']);

    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getAuthItemChildren0()
    {
        return $this->hasMany(AuthItemChild::className(), ['child' => 'name']);
    }

    /**
     * @return 返回后台管理员权限字符串
     */
    public function getAuthString()
    {
        $many = $this->authItemChildren;
//        echo '<pre>'; echo print_r($many);exit();
        $k = 0;
        $item = null;
        foreach ($many as $v){
            $itemone = $this::findOne(['name'=>$v['child']]);
//            if ($itemone['type']==1){
//               $itemchild = AuthItemChild::find()->where('parent=:parent',[':parent'=>$itemone['name']])->asArray()->all();
//               foreach ($itemchild as $vc){
//                   $itemone = $this::findOne(['name'=>$v['child']]);
//                   $item[$k++] = $vc['child'];
//               }
//            }else
            $item .= '   '.$itemone['description'];
        }
        if (empty($item))
            return '暂时未分配给权限';
        else
            return '拥有  '.$item.'  权限';

    }
}



















